#include <iostream>
#include "MsgInstanceClient.h"
#include "Communication.h"
#include "string.h"
#include "time.h"
#include <signal.h>
#include "EventLoop.h"
#include "HistoryRecord.h"
#include "Hotplug.h"
#include "ComLog.h"
#include "SerialCommunication.h"
#include "MsgInstanceServer.h"
class CTestMsgCenter
{
   public:
      CTestMsgCenter()
      {
          instance = new CMsgInstanceClient();

      }
      ~CTestMsgCenter()
      {

      

      }
      void test();
      void cb(char *msg, int msgLen, void *data);
   private:
      CMsgInstanceClient *instance;
};

void CTestMsgCenter :: cb(char *msg, int msgLen, void *data)
{
  
     printf("%s %d", msg, msgLen);

}


void CTestMsgCenter :: test()
{
    
   instance->MsgEventRegister(1, KEEPALIVE_EVENT, this, &CTestMsgCenter::cb, "dsadasdsa");
   instance->PrinteventList();
   instance->MsgEventUnRegister(1, KEEPALIVE_EVENT);
   instance->PrinteventList();
}

void sighandler(int signum)
{

     MY_ERROR_LOG(">>>>>>>>>>>>>>>>>>>>quit\n");
     exit(0);
}
class CTestEpoll
{
   public:
     CTestEpoll(CCommunication *cm){temp = cm;}
     ~CTestEpoll(){}
     void cb(int fd, void *data)
     {
         struct sockaddr_in faraddr = {0};
         int farfd = -1;
          if((farfd = temp->SocketAccept(&faraddr)) >= 0)
            {
                  MY_ERROR_LOG("client connect : ip %s : port %d\n", inet_ntoa(faraddr.sin_addr), faraddr.sin_port);
                  temp->SocketWrite(farfd, "dsadsadsa", strlen("dsadsadsa"));
                  sleep(2);
                  temp->SocketClose(farfd);
            }
     } 

     void test()
     {
            tempevent = new CEventLoop();
            tempevent->EventRegister(temp->SocketFdGet(), this, &CTestEpoll::cb, NULL);

     }

     void schedule()
     {
           tempevent->EventSchedule(30);

     }
   
   private:
   CCommunication *temp;
   CEventLoop *tempevent;
};

typedef union testmode_t
{
   int a;
   char b;
}testmode;

void HotplugCb1(const char *dev_str, eHotplugStatus status, void *data)
{
    MY_ERROR_LOG("%s %d\n", dev_str, status);


}
int main(void)
{
   CMsgInstanceServer *msgs = new CMsgInstanceServer();
   while(1)
   {
      msgs->MsgServerSchedule();
      usleep(100000);
   }
   #if  0
  // CTestMsgCenter *ct = new CTestMsgCenter();
  // ct->test();
   char *str = "dasdasdasdasdasdsadasasd";
   char *str1 = "dasdsadsad1321312312&&&&&&&&&&&&&&&&&&&&&dasdasdsa";
   char *str2 = "dasdsadsad1321312312&jjjj&&&&&&&&&&&&&&&&&&&&dasdasdsa";
   char hrd[1024] = {0};
   CHistoryRecord * cr = new CHistoryRecord();
   HistoryFileHeader *temp = cr->HistoryInit("./test.his", 20000);
   COMLOG_FATAL_LOG("%d %d %d\n", temp->maxrecord, temp->readindex, temp->writeindex);
   cr->HistoryAdd(str, strlen(str));
   COMLOG_FATAL_LOG("%d %d %d %d\n", temp->maxrecord, temp->readindex, temp->writeindex, cr->HistoryRemainCount());
   cr->HistorySetValid(0,0);
   cr->HistoryRead((char *)hrd);
   COMLOG_FATAL_LOG("%d %d %d %d\n", temp->maxrecord, temp->readindex, temp->writeindex, cr->HistoryRemainCount());
   COMLOG_FATAL_LOG("%s\n", hrd);
   cr->HistoryAdd(str1, strlen(str1));
   cr->HistoryAdd(str2, strlen(str2));
      cr->HistoryAdd(str1, strlen(str1));
   cr->HistoryAdd(str2, strlen(str2));
      cr->HistoryAdd(str1, strlen(str1));
   cr->HistoryAdd(str2, strlen(str2));
      cr->HistoryAdd(str1, strlen(str1));
   cr->HistoryAdd(str2, strlen(str2));
      cr->HistoryAdd(str1, strlen(str1));
   cr->HistoryAdd(str2, strlen(str2));
   COMLOG_FATAL_LOG("%d %d %d %d\n", temp->maxrecord, temp->readindex, temp->writeindex, cr->HistoryRemainCount());
   CHotplug *cp = new CHotplug();
   cp->HotplugInit();
   cp->HotplugRegisterEvent("ttyUSB0", HotplugCb1, NULL);

   if(SerialInit("/dev/tty0") >= 0)
   {
      COMLOG_ERROR_LOG("open tty0 successed\n");

   }
   while(1)
   {
      //  cr->HistoryRead((char *)hrd);
      //  MY_ERROR_LOG("%s\n", hrd);
      //  memset(hrd, 0, sizeof(hrd));
      //  MY_ERROR_LOG("%d %d %d %d\n", temp->maxrecord, temp->readindex, temp->writeindex, cr->HistoryRemainCount());
       cp->HotplugSchedule(NULL);
       usleep(1000 * 100);
   }
  
   signal(SIGQUIT,sighandler);
   signal(SIGABRT,sighandler);
   signal(SIGINT,sighandler);
   CCommunication *cm = new CCommunication();
   int re = cm->SocketIntanceTCP(SOCKET_SERVER, NULL, 50000);
   MY_ERROR_LOG("re : %d fd:%d \n", re, cm->SocketFdGet());
   cm->SocketListen(10);
   CTestEpoll *ce = new CTestEpoll(cm);
   ce->test();
   while(1)
   {
      ce->schedule();
   }
  
   fd_set fs;
 
   
   struct sockaddr_in faraddr = {0};
   int farfd = -1;
   while(1)
   {
       FD_ZERO(&fs);
       FD_SET(cm->SocketFdGet(), &fs);
       struct timeval tm = {0};
       tm.tv_sec = 3;
       tm.tv_usec = 0;
       int ret = select(cm->SocketFdGet() + 1, &fs, NULL, NULL, &tm);
       //MY_ERROR_LOG("cm->SocketFdGet(): %d ret: %d\n", cm->SocketFdGet(), ret);
       if(ret > 0)
       {
          if(FD_ISSET(cm->SocketFdGet(), &fs))
          {
               if((farfd = cm->SocketAccept(&faraddr)) >= 0)
                {
                       MY_ERROR_LOG("client connect : ip %s : port %d\n", inet_ntoa(faraddr.sin_addr), faraddr.sin_port);
                       cm->SocketWrite(farfd, "dsadsadsa", strlen("dsadsadsa"));
                       sleep(2);
                       cm->SocketClose(farfd);
                }
               //else
                 // MY_ERROR_LOG("wating1\n");
          }
          else
             MY_ERROR_LOG("wating2\n");
 
       }
       else if(ret == 0)
       {

            MY_ERROR_LOG("wating4\n");
       }
       else
       {
             MY_ERROR_LOG("wating3\n");
             continue;          
       }
   }
   #endif
   return 0;
}